//公用的函数库--依赖jQuery
//跟数据相关的都放入comm
function getUserInfo(token, succ, erro) {
    $.ajax({
        url: 'http://www.liulongbin.top:3007/my/userinfo',
        method: 'GET',
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Authorization', token)
        },
        success: function(data) {
            let userinfo = data.data
            succ(userinfo)
        },
        erro: function(data) {
            erro(data)
        }
    })
}

//重置密码

function resetPwd(token, oldPwd, newPwd, succ, erro) {
    $.ajax({
        url: 'http://www.liulongbin.top:3007/my/updatepwd',
        method: 'POST',
        data: {
            oldPwd: oldPwd,
            newPwd: newPwd
        },
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Authorization', token)
        },
        success: function(data) {
            succ(data)
        },
        erro: function(data) {
            erro(data)
        }
    })
}

//更换头像
function updateImg(token, base64Img, succ, erro) {
    $.ajax({
        url: 'http://www.liulongbin.top:3007/my/update/avatar',
        method: 'POST',
        data: {
            avatar: base64Img
        },
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Authorization', token)
        },
        success: function(data) {
            succ(data)
        },
        erro: function(data) {
            erro(data)
        }
    })
}

//取出文章分类列表

function getAticList(token, succ, erro) {
    $.ajax({
        url: 'http://www.liulongbin.top:3007/my/article/cates',
        method: 'GET',
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Authorization', token)
        },
        success: function(data) {
            succ(data)
        },
        erro: function(data) {
            erro(data)
        }
    })
}


//提交新分类
function addAtic(token, data, succ, erro) {
    $.ajax({
        url: 'http://www.liulongbin.top:3007/my/article/addcates',
        method: 'POST',
        data: data,
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Authorization', token)
        },
        success: function(data) {
            succ(data)
        },
        erro: function(data) {
            erro(data)
        }
    })
}


//删除功能

function delAtic(token, id, succ, erro) {
    $.ajax({
        url: 'http://www.liulongbin.top:3007/my/article/deletecate/' + id,
        method: 'GET',
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Authorization', token)
        },
        success: function(data) {
            succ(data)
        },
        erro: function(data) {
            erro(data)
        }
    })
}

//编辑文章类别
//data中包含id，name，alias
function updateAtic(token, data, succ, erro) {
    $.ajax({
        url: 'http://www.liulongbin.top:3007/my/article/updatecate',
        method: 'POST',
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Authorization', token)
        },
        data: data,
        success: function(data) {
            succ(data)
        },
        erro: function(data) {
            erro(data)
        }
    })
}


//发送
function addNewAtic(token, data, succ, erro) {
    $.ajax({
        url: 'http://www.liulongbin.top:3007/my/article/add',
        method: 'POST',
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Authorization', token)
        },
        contentType: false,
        processData: false,
        data: data,
        success: function(data) {
            succ(data)
        },
        erro: function(data) {
            erro(data)
        }
    })
}

//按Id分类获取相应的文章
// pagenum	是	int	页码值
// pagesize	是	int	每页显示多少条数据
// cate_id	否	string	文章分类的 Id
// state	否	string	文章的状态，可选值有：已发布、草稿
function getIdAticle(token, data, succ, erro) {
    $.ajax({
        url: 'http://www.liulongbin.top:3007/my/article/list',
        method: 'GET',
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Authorization', token)
        },
        data: data,
        success: function(data) {
            succ(data)
        },
        erro: function(data) {
            erro(data)
        }
    })
}


// 删除功能

function delIdAticle(token, id, succ, erro) {
    $.ajax({
        url: 'http://www.liulongbin.top:3007/my/article/delete/' + id,
        method: 'GET',
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Authorization', token)
        },
        success: function(data) {
            succ(data)
        },
        erro: function(data) {
            erro(data)
        }
    })
}
